
#include "InstrumentCache.h"

using namespace System;
using namespace System::Collections::Generic;
using namespace System::Runtime::InteropServices;
using namespace System::Globalization;

using namespace Tradex::Market;

namespace Tradex {
	namespace Connectivity {
		namespace NxCore {

			InstrumentCache::InstrumentCache(void) {
			}

			void InstrumentCache::SetInstrumentBasics (String^ market, const NxCoreSystem* nxCoreSys, const NxCoreMessage* nxCoreMsg) {
				const char prefix = nxCoreMsg->coreHeader.pnxStringSymbol->String[0];
				switch (prefix) {
				case 'b': // Bond
					_InstrumentType = InstrumentType::Bond;
					break;
				case 'c': // Currency
					_InstrumentType = InstrumentType::Currency;
					break;
				case 'e': // Equity
					_InstrumentType = InstrumentType::Equity;
					break;
				case 'f': // Future
					_InstrumentType = InstrumentType::Future;
					break;
				case 'i': // Index
					_InstrumentType = InstrumentType::Index;
					break;
				case 'm': // Mutual Fund
					_InstrumentType = InstrumentType::Fund;
					break;
				case 'o': // Equity or Index option root... not used for options on futures
					_InstrumentType = InstrumentType::Product;
					break;
				case 'p': // Future option
					_InstrumentType = InstrumentType::Option;
					break;
				case 'r': // Root Symbol /for Futures / Futures Options
					_InstrumentType = InstrumentType::Product;
					break;
				case 's': // Single Stock Futures
					_InstrumentType = InstrumentType::Future;
					break;
				case 'z': // Spread
					_InstrumentType = InstrumentType::Spread;
					break;
				}
				String^ symbol;
				if (prefix != 'r') {
					symbol = gcnew System::String (nxCoreMsg->coreHeader.pnxStringSymbol->String +1);
				} else {
					symbol = gcnew System::String (nxCoreMsg->coreHeader.pnxStringSymbol->String +2);
				}
				if (prefix == 'z') {
					symbol = symbol->Replace ('_', '-');
				}

				_InstrumentKey = gcnew InstrumentKey (market, symbol);
			}


			bool InstrumentCache::RegisterBid (DomQuote^ bid) {
				if (_BidArray == nullptr) {
					_BidArray = gcnew array<DomQuote^>(1);
					_BidArray[0] = bid;
					return true;
				}
				bool retval = false;
				int index = 0;
				for (;index < _BidArray->Length; index++) {
					DomQuote^ pArrayQuote = _BidArray[index];
					if (pArrayQuote->Level == bid->Level) {
						if (pArrayQuote->Price == bid->Price) {
							if (pArrayQuote->Volume == bid->Volume) {
								return false;
							} else {
								return true;
							}
						} else {
							
						}
					} else {
					}
				}
				return false;
			}

			array<DomQuote^>^ InstrumentCache::ProcessDeletedBids () {
				return nullptr;
			}

			bool InstrumentCache::RegisterAsk (DomQuote^ bid) {
				return false;
			}

			array<DomQuote^>^ InstrumentCache::ProcessDeletedAsks () {
				return nullptr;
			}

		}
	}
}